#include <iostream>
#include <stdlib.h>
using namespace std;

class Singleton{
public:
    static Singleton * getInstance()
    {
        if(_pInstance == nullptr){
            //程序退出时候释放资源atexit函数
            //来注册程序正常终止时要被调用的函数
             atexit(destroy);
            _pInstance = new Singleton();
        }
        return _pInstance;
    }
private:
    static void destroy(){
        if(_pInstance){
            delete _pInstance;
            _pInstance = nullptr;
        }
    }
    Singleton(){
        cout<<"Singleton()"<<endl;
    }
    ~Singleton(){
        cout<<"~Singleton()"<<endl;
    }
    static Singleton * _pInstance;
};
Singleton * Singleton::_pInstance = nullptr;
void test(){
    Singleton *ps = Singleton::getInstance();
}
int main()
{
    test();
    return 0;
}

